package com.pay.game.central.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.fasterxml.uuid.Generators;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pay.game.central.common.Result;
import com.pay.game.central.common.ResultGenerator;
import com.pay.game.central.security.annotaion.UserLoginToken;
import com.pay.game.central.web.bean.PtScore;
import com.pay.game.central.web.service.PtScoreService;
import com.pay.game.central.web.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("score")
public class ScoreController {

    @Autowired
    PtScoreService scoreService;
    @Autowired
    StoreService storeService;

//    @PostMapping("/add")
//    @ResponseBody
//    @UserLoginToken
//    Result<String> addScore(@RequestBody String body,@RequestHeader Map<String, String> headers){
//        String token = headers.get("x-token");
//
//        // 获取操作用户
//        String userId = JWT.decode(token).getAudience().get(0);
//
//        JSONObject jsonObject = JSON.parseObject(body);
//        String storeId = jsonObject.getString("storeId");
//        int score = jsonObject.getInteger("score");
//        //为店铺上分
//        storeService.addScore(storeId,score);
//
//        PtScore ptScore = new PtScore();
//        ptScore.setId(Generators.timeBasedGenerator().generate().toString());
//        ptScore.setOpt_time(System.currentTimeMillis());
//        ptScore.setStore_id(storeId);
//        ptScore.setValue(score);
//        ptScore.setUser_id(userId);
//        //保存记录
//        scoreService.insertItem(ptScore);
//        return ResultGenerator.genSuccessResult("操作成功");
//    }

    @GetMapping("/list")
    @ResponseBody
    @UserLoginToken
    Result<PageInfo<Map<String,Object>>> list(int page,int limit,String storeId, String storeName, String username){
        PageHelper.startPage(page, limit);
        PageInfo<Map<String,Object>> list = new PageInfo<>(scoreService.list(storeId, storeName, username));
        return ResultGenerator.genSuccessResult(list);
    }

    @GetMapping("/info")
    @ResponseBody
    @UserLoginToken
    Result<Map<String, Integer>> info(String userId){
        return ResultGenerator.genSuccessResult(scoreService.scoreInfo(userId));
    }
}
